﻿using System;
using System.Collections.Generic;

using IBatisNet.DataMapper;

using Yawf.Core.Logs;
using IBatisNet.DataMapper.SessionStore;

namespace Yawf.Data {
	public class IbatisPersister : IPersister{
		private SqlMapper sqlMapper;

		public IbatisPersister() {
		}

		public IbatisPersister(SqlMapper mapper){
			mapper.SessionStore = new HybridWebThreadSessionStore(mapper.Id);
			SQLMapper = mapper;
		}

		public SqlMapper SQLMapper { get { return sqlMapper; } set { sqlMapper = value; } }

		/// <summary>
		/// Executes a Ibatis statement and gets the first object found
		/// </summary>
		/// <param name="query">Name of Ibatos statement</param>
		/// <param name="parameters">Parameter(s) to use</param>
		/// <returns>Object found, null if query does not return anything</returns>
		public T FindOne<T>(String query,Object parameters) {
			IList<T> hits;

			hits = SQLMapper.QueryForList<T>(query,parameters);
			if (hits != null && hits.Count > 0) {
				return hits[0];
			}

			return default(T);
		}									// FindOne

		/// <summary>
		/// Finds a list of objects by executing a Ibatis statement
		/// </summary>
		/// <param name="query">Ibatis statement to exeute</param>
		/// <param name="parameters">Parameter(s) to set</returns>
		public IList<T> FindList<T>(String query,Object parameters) {
			return FindList<T>(query,parameters,1,Int32.MaxValue);
		}									// FindList

		/// <summary>
		/// Finds a list of objects by executing a Ibatis statement
		/// </summary>
		/// <param name="query">Ibatis statement to exeute</param>
		/// <returns>A list of objects found</returns>
		public IList<T> FindList<T>(String query) {
			return FindList<T>(query,null,1,Int32.MaxValue);
		}									// FindList

		/// <summary>
		/// Finds a list of objects by executing a Ibatis statement to 
		/// </summary>
		/// <param name="query">Ibatis statement to exeute</param>
		/// <param name="parameters">Parameter(s) to set</param>
		/// <param name="pageIndex">Page index to get, starts from 1</param>
		/// <param name="pageSize">Page size to return</param>
		/// <returns>A list of objects found</returns>
		public IList<T> FindList<T>(String query,Object parameters,int pageIndex,int pageSize) {
			try {
				return SQLMapper.QueryForList<T>(query,parameters,(pageIndex - 1) * pageSize,pageSize);
			} catch (Exception e) {
				Log.ExceptionMessage(this.GetType(), "Error executing Ibatis statement " + query + " in FindList", e);
			}

			return null;
		}									// FindList

		/// <summary>
		/// Exewcutes a Deletes Ibatis statement
		/// </summary>
		/// <param name="query">Ibatis statement to execute</param>
		/// <param name="o">Parameters</param>
		public void Delete(String query,Object o) {
			SQLMapper.Delete(query,o);
		}									// Delete

		/// <summary>
		/// Updates an Object in database
		/// </summary>
		/// <param name="query">Ibatis statement to execute</param>
		/// <param name="o">Parameters</param>
		public void Update(String query,Object o) {
			SQLMapper.Update(query,o);
		}									// Update

		/// <summary>
		/// Inserts an Object into database
		/// </summary>
		/// <param name="query">Ibatis statement to execute</param>
		/// <param name="o">Parameters</param>
		public void Insert(String query,Object o) {
			SQLMapper.Insert(query,o);
		}									// Insert
	}
}
